package com.stock;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.PostMethod;

import com.dalin.common.utils.NumberUtils;
import com.dalin.common.utils.StringUtils;
import com.dalin.common.utils.TextUtils;
import com.stock.dao.DiaryDataDao;
import com.stock.dao.StockInfoDao;

public class DownloadData {

	public static void main(String[] args) {
		//updateDataToDbByCode("000002");
		for (StockProp sp : StockInfoDao.queryStocks()) {
			updateDataToDbByCode(sp.getCode());
		}
		//getLimitUpStocks();
	}

	public static List<StockProp> getStockPool() {
		List<StockProp> rtnList = new ArrayList<StockProp>();
		String initCode = "600001";
		try {
			/*for (int i = 0; i < 1000000; i++) {
				String rtn = getCurrentData(initCode);
				System.out.println(initCode);
				if (rtn != null) {
					String s1 = rtn.split("\"")[1].split(",")[0];
					System.out.println("=="+s1+"=="+rtn);
					StockProp sp = new StockProp();
					sp.setCode(initCode);
					sp.setName(s1);
					StockInfoDao.updateStockInfo(sp);
					rtnList.add(sp);
				}
				initCode = NumberUtils.getNextStrNum(initCode);
			}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtnList;
	}
	
	public static List<StockProp> getCurrentData(String codes) {
		try {
			String[] codeAry = codes.split(",");
			StringBuffer sb = new StringBuffer("");
			if (codeAry.length == 1) {
				sb.append((codes.startsWith("60") ? "sh" : "sz") + codes+",");
			} else {
				for (String code : codeAry) {
					if (!StringUtils.isBlank(code))
						sb.append((code.startsWith("60") ? "sh" : "sz") + code+",");
				}
			}
			HttpClient httpClient = new HttpClient();
			String url = "http://hq.sinajs.cn/list="
					+ sb.toString();
			//System.out.println(url);
			HttpMethodBase method = new PostMethod(url);
			method.setRequestHeader("Content-Type",
					"application/json;charset=UTF-8");
			httpClient.getHttpConnectionManager().getParams()
					.setConnectionTimeout(50000);
			httpClient.executeMethod(method);
			String response = method.getResponseBodyAsString();
			//System.out.println(response);
			method.releaseConnection();
			if ("\"\";\n".equals(response.split("=")[1])) {
				return null;
			}
			List<StockProp> rtnList = new ArrayList<StockProp>();
			String[] ary = response.split("\n");
			for (String s : ary) {
				String[] pary = s.split(",");
				//System.out.println(s+">"+pary.length);
				if (pary.length==1) continue;
				StockProp sp = new StockProp();
				sp.setName(pary[0].split("=\"")[1]);
				sp.setCode(s.substring(13,19));
				sp.setLimitUp((double)(Math.round((Double.parseDouble(pary[2])*1.1)*100)/100.0));
				sp.setValue((double)(Math.round((Double.parseDouble(pary[3]))*100)/100.0));
				sp.setIncrease((double)(Math.round((Double.parseDouble(pary[3]) / Double.parseDouble(pary[2])  -1)*10000)/100.0));
				rtnList.add(sp);
			}
			return rtnList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void updateDataToDbByCode(String code) {
		try {
			Map m = DiaryDataDao.queryDiaryData(code, "2015-06-28");
			if(m != null) return;
			String res = DiaryDataDao.getInternetHistoryByCode(code);
			System.out.println("=="+res);
			String[] ta = res.split("\n");
			for (int i = 1; i < ta.length;i++) {
				if (StringUtils.isBlank(ta[i])) continue;
				String[] sr = ta[i].split(",");
				StockProp sp = new StockProp();
				sp.setCode(code);
				sp.setDate(sr[0]);
				sp.setOpen(Double.parseDouble(sr[1]));
				sp.setHigh(Double.parseDouble(sr[2]));
				sp.setLow(Double.parseDouble(sr[3]));
				sp.setClose(Double.parseDouble(sr[4]));
				sp.setVolume(Long.parseLong(sr[5]));
				DiaryDataDao.update(sp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static String getLimitUpStocks() {
		StringBuffer codes = new StringBuffer("");
		try {
			List<StockProp> stockList = StockInfoDao.queryStocks();
			StringBuffer sb = new StringBuffer("");
			for (int i=0; i < stockList.size();i++) {
				sb.append(stockList.get(i).getCode()+",");
				if (i % 400 == 0) {
					List<StockProp> cl = getCurrentData(sb.toString());
					for (StockProp sp : cl) {
						//System.out.println(sp.getCode()+"="+sp.getLimitUp() +"="+ sp.getValue());
						if (sp.getValue().equals(sp.getLimitUp())) {
							codes.append(sp.getCode()+",");
						}
					}
					sb.delete(0, sb.length()-1);
				}
			}
			return codes.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//涨速最快的
	public static String getTopSpeed() {
		StringBuffer codes = new StringBuffer("");
		try {
			List<StockProp> stockList = StockInfoDao.queryStocks();
			StringBuffer sb = new StringBuffer("");
			for (int i=0; i < stockList.size();i++) {
				sb.append(stockList.get(i).getCode()+",");
				if (i % 400 == 0) {
					List<StockProp> cl = getCurrentData(sb.toString());
					for (StockProp sp : cl) {
						
					}
					sb.delete(0, sb.length()-1);
				}
			}
			return codes.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}